package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.po.AxhFinanceLog;
import com.xueyi.aixihu.entity.po.CdzAuditRecord;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.reqeust.FindReportFormListReq;
import com.xueyi.aixihu.reqeust.UploadVoucherReq;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author hxs
 * @createDate 2023/6/28 11:40
 * @description 上传报表相关接口
 */
public interface ReportFormsMapper {
    @Insert({
            " INSERT INTO `axh_finance` ( source_file_url , `finance_title`, `from_to_area_id`, `current_status`, `create_user_id`, `area_type`, `report_images`, `report_type` , `system_type_id` )  " +
                    "VALUES  " +
                    "  ( #{item.sourceFileUrl} , #{item.financeTitle} , #{item.fromToAreaId} , #{item.currentStatus} , " +
                    "#{item.createUserId} , #{item.areaType} , #{item.reportImages} , #{item.reportType} , #{item.systemTypeId} ); "
    })
    void saveReportForms(@Param("item") AxhFinanceVo flag);

    @Update({
            " update  axh_finance  set  " +
            "`finance_title`=   #{item.financeTitle}, " +
            "`create_time`=   #{item.createTime}, " +
            "`last_update_time`=#{item.lastUpdateTime}  ," +
            "`report_proof_image`=#{item.reportProofImage}  ," +
            "`system_type_id`=#{item.systemTypeId}  " +
            " where id=#{item.id}"
    })
    void updateReportForms(@Param("item") AxhFinanceVo flag);

    /**
     * 查询报表列表
     *
     * @param findReportFormListReq 参数
     * @param userId                用户id
     * @param areaIds
     * @return 列表
     */
    @Select({
            "<script> select tab.* , du.username createUserIdStr, dns.name fromToAreaIdStr from ( select * from axh_finance   " +
                    " where report_type = #{item.reportType} and is_valid =1    " +
                    " <if test = 'item.areaId != null and item.areaId!=\"36\" '>" +
                    " and create_user_id = #{userId}  " +
                    " </if>" +
                    " <if test = 'item.month != null'>" +
                    " and DATE_FORMAT(create_time,'%Y-%m') = #{item.month} " +
                    " </if>  " +
                    " <if test = 'item.currentStatusStr != null'>" +
                    " and current_status in (${item.currentStatusStr} ) " +
                    " </if> " +
                    " <if test = 'item.systemTypeId != null'>" +
                    " and system_type_id = #{item.systemTypeId} " +
                    " </if> " +
                    " <if test = 'item.keyword != null'>" +
                    " and finance_title like '%${item.keyword}%' " +
                    " </if> " +
                    " <if test = 'item.areaId != null'>" +
                    " and from_to_area_id = #{item.areaId} " +
                    " </if>" +
                    " <if test = 'areaIds != null'>" +
                    " and from_to_area_id in (${areaIds})  " +
                    " </if>" +
                    " <if test = 'item.systemTypeId != null'>" +
                    " and system_type_id = #{item.systemTypeId} " +
                    " </if>" +
                    " order by  create_time desc " +
                    " limit #{item.startIndex} , #{item.limit}" +
                    " ) tab left join dt_users du on tab.create_user_id = du.id " +
                    " left join dt_news_sort dns on tab.from_to_area_id = dns.id " +
                    " </script>"
    })
    List<AxhFinanceVo> findReportFormList(@Param("item") FindReportFormListReq findReportFormListReq, @Param("userId") Integer userId, @Param("areaIds") String areaIds);

    /**
     * 查询报表列表总条数
     *
     * @param findReportFormListReq 参数
     * @param userId                用户id
     * @param areaIds
     * @return 总条数
     */
    @Select({
            "<script> select count(id) from axh_finance   " +
                    " where report_type = #{item.reportType} and is_valid =1   " +
                    " <if test = 'item.areaId != null and item.areaId!=\"36\" '>" +
                    " and create_user_id = #{userId}  " +
                    " </if>" +
                    " <if test = 'item.month != null'>" +
                    " and DATE_FORMAT(create_time,'%Y-%m') = #{item.month} " +
                    " </if>  " +
                    " <if test = 'item.currentStatusStr != null'>" +
                    " and current_status in (${item.currentStatusStr} ) " +
                    " </if> " +
                    " <if test = 'item.systemTypeId != null'>" +
                    " and system_type_id = #{item.systemTypeId} " +
                    " </if> " +
                    " <if test = 'item.keyword != null'>" +
                    " and finance_title like '%${item.keyword}%' " +
                    " </if> " +
                    " <if test = 'item.systemTypeId != null'>" +
                    " and system_type_id = #{item.systemTypeId} " +
                    " </if>" +
                    " <if test = 'areaIds != null'>" +
                    " and from_to_area_id in (${areaIds})  " +
                    " </if>" +
                    " <if test = 'item.areaId != null'>" +
                    " and from_to_area_id = #{item.areaId} " +
                    " </if> </script>"
    })
    Integer findReportFormListTotal(@Param("item") FindReportFormListReq findReportFormListReq, @Param("userId") Integer userId, @Param("areaIds") String areaIds);

    /**
     * 通过id查询报表对象
     *
     * @param id id
     * @return 报表
     */
    @Select({
            " select * from axh_finance where id = #{id} "
    })
    AxhFinanceVo findReportFormById(@Param("id") Integer id);

    /**
     * 逻辑删除报表
     *
     * @param id 报表id
     */
    @Update({
            " update axh_finance set is_valid = 0 where id = #{id} "
    })
    void deleteReportForm(@Param("id") Integer id);

    /**
     * 修改报表状态
     *
     * @param financeVo 报表对象
     */
    @Update({
            " update axh_finance set current_status = #{item.currentStatus}  where id = #{item.id} "
    })
    void updateReportFormsAuditStatus(@Param("item") AxhFinanceVo financeVo);

    /**
     * 添加报表审核记录
     *
     * @param record 记录对象
     */
    @Insert({
            " insert into cdz_audit_record ( audit_type , join_id , submit_user_id , current_status ) " +
                    " values ( #{item.auditType} , #{item.joinId} , #{item.submitUserId} , #{item.currentStatus} ) "
    })
    void addReportFormsAuditRecord(@Param("item") CdzAuditRecord record);

    /**
     * 修改审核记录的公开状态
     *
     * @param record 审核记录
     */
    @Update({
            " update cdz_audit_record set is_public = #{ item.isPublic } where id = #{item.id} "
    })
    void publishReportForm(@Param("item") CdzAuditRecord record);

    /**
     * 修改报表记录
     *
     * @param record 审核记录
     */
    @Update({
            " update axh_finance set is_public = #{isPublic} where id = #{id} "
    })
    void updateReportFromIsPublic(@Param("id") Integer id ,
                                  @Param("isPublic") Integer isPublic);

    /**
     * 通过id查询报表审核记录
     *
     * @param id 审核记录id
     * @return 审核记录
     */
    @Select({
//            " select * from cdz_audit_record where id = #{id} "
            " select tab.* , af.area_type ,af.finance_title from ( select * from cdz_audit_record where join_id = #{id} order by submit_time desc limit 1 ) tab " +
                    " left join axh_finance af on tab.join_id = af.id  "

    })
    CdzAuditRecordVo findAuditRecordById(@Param("id") Integer id);

    /**
     * 修改报表审核记录状态
     *
     * @param auditId       审核记录id
     * @param currentStatus 需要修改的状态
     */
    @Update({
            " update cdz_audit_record set current_status = #{currentStatus} , fail_msg = #{msg} where id = #{auditId} "
    })
    void updateReportFormsAuditRecordStatus(@Param("auditId") Integer auditId, @Param("currentStatus") Integer currentStatus, @Param("msg") String msg);

    /**
     * 插入报表日志
     *
     * @param axhFinanceLog 对象
     */
    @Insert({
            " insert into axh_finance_log ( finance_id , command_user_id , log_content , current_finance_status , remark  , log_type) " +
                    " values ( #{item.financeId} , #{item.commandUserId} , #{item.logContent} , #{item.currentFinanceStatus} , #{item.remark}  , #{item.logType}) "
    })
    void addFinanceLog(@Param("item") AxhFinanceLogVo axhFinanceLog);

    /**
     * 查询财务报表类型列表
     *
     * @param type 类型
     * @return 财务报表类型列表
     */
    @Select({
            " select id `value` , `name` label from cdz_system_type where is_valid = 1 and pid = #{pid} and type = #{type} "
    })
    List<BaseSelectVo> findCwTypeList(@Param("type") Integer type, @Param("pid") Integer pid);

    @Select({
            "<script> "+
            " select     " +
                    "    count(id) allSize,    " +
                    "    count(IF(current_status in (${agree}),id,null)) agreeSize,    " +
                    "    count(IF(current_status in (${audit}),id,null)) auditSize,    " +
                    "    count(IF(current_status in (${refuse}),id,null)) refuseSize,    " +
                    "    count(IF(current_status in (${draft}),id,null)) draftSize    " +
                    " from axh_finance where is_valid = 1 " +
                    "<if test='areaIds!=\"36\"'>"+
                    "and create_user_id = ${id} " +
                    "</if> "+
                    "and from_to_area_id in (${areaIds}) and report_type = ${reportType} "+
            "</script> "
    })
    FindReportFormsSizeVo findReportFormsSize(@Param("id") Integer id, @Param("areaIds") String areaIds,
                                              @Param("reportType") Integer reportType, @Param("agree") String agree,
                                              @Param("audit") String audit, @Param("refuse") String refuse, @Param("draft") String draft);

    /**
     * 操作凭证
     *
     * @param uploadVoucherReq 参数
     */
    @Update({
            " update axh_finance set report_proof_image = #{item.img} where id = #{item.id} "
    })
    void uploadVoucher(@Param("item") UploadVoucherReq uploadVoucherReq);

    /**
     * 查询审核记录
     *
     * @param id         id
     * @param reportType 类型
     */
    @Select({
            " select * from cdz_audit_record where join_id = #{id} and audit_type = #{reportType} and is_valid = 1 order by submit_time desc limit 1 "
    })
    CdzAuditRecord findAuditRecord(@Param("id") Integer id, @Param("reportType") Integer reportType);

    /**
     * 修改报表审核记录
     *
     * @param record 参数
     */
    @Update({
            " update cdz_audit_record set submit_user_id = #{item.submitUserId} , submit_time = now() , current_status = #{item.currentStatus} , last_command_time = now() , " +
                    " fail_msg = null where id = #{item.id} "
    })
    void updateReportFormsAuditRecord(@Param("item") CdzAuditRecord record);
}
